home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / 13h_kit / getfont.cpp < prev    next >
C/C++ Source or Header  |  1991-07-04  |  4KB  |  132 lines

  1. // GETFONT.CPP
  2. // small program to display and grab font bitmap data from a PCX file.
  3.  
  4. #include <dos.h>
  5. #include <io.h>
  6. #include <stdio.h>
  7. #include <conio.h>
  8. #include <alloc.h>
  9. #include <mem.h>
  10. #include <fcntl.h>
  11. #include <sys/stat.h>
  12. #include "keyboard.hpp"
  13. #include "images.hpp"
  14.  
  15. // fill in the constants below as applicable to the font you're capturing
  16.  
  17. struct header {                     // font description structure
  18.     char  font_name[8];             // font name, no file extension
  19.     char  fram_wid;                 // width of character frame in pixels
  20.     char  fram_hit;                 // height of character frame in pixels
  21.     char  base_ofs;                 // baseline y offset from top
  22.     char  ascii_first;              // number of first character defined
  23.     char  ascii_last;               // number of last character defined
  24.     char f_color;          // original font foreground color
  25.  
  26. } HDR={"CSFONT1",6,6,5,0x20,0x7F,15};  // <<-----FILL IN HERE
  27.  
  28. char width[128] = {4,2,3,5,5,5,5,2,2,2,2,4,2,2,2,5,4,2,4,4,4,4,4,4,4,
  29.                    4,2,2,5,4,5,4,5,4,4,4,4,4,4,4,4,3,4,4,4,5,5,4,4,4,
  30.                    4,4,4,4,4,5,5,4,4,2,5,2,5,5,2,5,4,4,4,4,3,4,4,3,4,
  31.                    4,2,5,4,4,4,4,4,4,3,4,4,5,4,4,4,2,2,2,4,5};
  32.  
  33. char cursor_mask[6][6]= {0,0,1,0,0,0,
  34.                          0,1,0,1,0,0,
  35.                          1,0,0,0,1,0,
  36.                          0,1,0,1,0,0,
  37.                          0,0,1,0,0,0,
  38.                          0,0,0,0,0,0};
  39.  
  40.  
  41. const short x_origin=20;            // top left x of first frame in each row
  42. const short row1_y_origin=24;       // top left y of first frame in row #
  43. const short row2_y_origin=31;
  44. const short row3_y_origin=38;
  45. const short chars_per_row=32;          // number of frames in each row
  46.  
  47. char pcxfile[80]="FONT1.PCX";    // source pcx file
  48. char fntfile[80]="CSFONT1.FNT";  // destination font file name
  49. char result;
  50.  
  51. const void far *vbuff=MK_FP(0xA000,0);
  52.  
  53. unsigned int i,x,y;
  54.  
  55. // **************************************************************************
  56. // grabchar() gets the character at the passed x,y coord and writes it to
  57. // the file indicated by the global handle.
  58. // **************************************************************************
  59.  
  60. void grabchar(int x, int y) {
  61.     int j;
  62.     for(j=y;j<(y+HDR.fram_hit);j++) {
  63.         write(handle,xy_to_ptr(x,j),HDR.fram_wid);
  64.     }
  65. }
  66.  
  67.  
  68. void main() {
  69.     _setgraphmode();
  70.  
  71.     // init the pcx file
  72.  
  73.     pcx font("font1.pcx");
  74.     if (font.getstatus() != NoErr) {
  75.         reporterr(font.getstatus(),"main()");
  76.         return;
  77.     }
  78.     result = font.load(Bestfit);
  79.     if ((result != NoErr) && (result != MemErr)) {
  80.         reporterr(result,"font.load()");
  81.         return;
  82.         }
  83.     font.display(SnapWipe);
  84.  
  85.     // create the font file in the current directory
  86.  
  87.     if((handle=open(fntfile,O_CREAT|O_BINARY,S_IREAD|S_IWRITE))==-1) {
  88.         _settextmode();
  89.         printf("error creating font file\n");
  90.         _settextmode();
  91.         return;
  92.     }
  93.  
  94.     // write the font header, character width table, and cursor mask to
  95.     // the new file
  96.  
  97.     write(handle,&HDR,sizeof(HDR));
  98.     write(handle,&width[0],sizeof(width));
  99.     write(handle,&cursor_mask[0][0],sizeof(cursor_mask));
  100.  
  101.     // capture row 1
  102.  
  103.     for (i=0;i<chars_per_row;i++) {
  104.         x=(i*HDR.fram_wid)+x_origin;
  105.         grabchar(x,row1_y_origin);
  106.     }
  107.  
  108.     // capture row 2
  109.  
  110.     for (i=0;i<chars_per_row;i++) {
  111.         x=(i*HDR.fram_wid)+x_origin;
  112.         grabchar(x,row2_y_origin);
  113.     }
  114.  
  115.     // capture row 3
  116.  
  117.     for (i=0;i<chars_per_row;i++) {
  118.         x=(i*HDR.fram_wid)+x_origin;
  119.         grabchar(x,row3_y_origin);
  120.     }
  121.  
  122.     // cleanup
  123.  
  124.     sound(100);
  125.     delay(200);
  126.     nosound();
  127.     while(!kbhit());
  128.     _settextmode();
  129.     return;
  130. }
  131.  
  132.